package com.zxc.oj.model.enums;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.extern.java.Log;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 语言类型类型枚举
 */
@Log
public enum QuestionSubmitLanguageEnum {

    JAVA("java", "java"),
    CPLUSPLUS("cpp", "cpp"),
    GOLANG("go", "go");

    private final String text;

    private final String value;

    QuestionSubmitLanguageEnum(String text, String value) {
        this.text = text;
        this.value = value;
    }

    /**
     * 获取值列表
     *
     * @return
     */
    public static List<String> getValues() {
        return Arrays.stream(values()).map(item -> item.value).collect(Collectors.toList());
    }

    /**
     * 根据 value 获取枚举
     *
     * @param value
     * @return
     */
    public static QuestionSubmitLanguageEnum getEnumByValue(String value) {
        if (ObjectUtil.isEmpty(value)) {
            return null;
        }
        for (QuestionSubmitLanguageEnum anEnum : QuestionSubmitLanguageEnum.values()) {
            if (anEnum.value.equals(value)) {
                return anEnum;
            }
        }
        return null;
    }

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static QuestionSubmitLanguageEnum forValue(String value) {
        log.info("forValue: " + value);
        for (QuestionSubmitLanguageEnum lang : values()) {
            if (lang.getValue().equalsIgnoreCase(value)) {
                return lang;
            }
        }
        throw new IllegalArgumentException("Unknown language: " + value);
    }

    @JsonValue
    public String getValue() {
        return value;
    }


    public String getText() {
        return text;
    }
}
